package com.Web.DAO.Impl;

import com.Web.DAO.IStudentDAO;
import com.Web.Person.Query.StudentQuery;
import com.Web.Person.Student;
import com.Web.Person.VO.StudentVO;
import com.Web.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDAOImpl implements IStudentDAO {
    @Override
    public List<StudentVO> selectByPage(StudentQuery studentQuery) {
        System.out.println("selectByPage");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<StudentVO> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            //String sql = "SELECT s.id,s.`name`,s.age,s.gender,b.`name`AS banjiName FROM student AS s LEFT JOIN banji as b ON s.banji_id=b.id LIMIT ?,?";
            String sql = "SELECT s.id,s.`name`,s.age,s.gender,b.`name`AS banjiName FROM student AS s LEFT JOIN banji as b ON s.banji_id=b.id WHERE 1=1";
            List<Object> queryList = new ArrayList<>();
            String queryName = studentQuery.getName();
            if(queryName != null && !"".equalsIgnoreCase(queryName)) {
                sql += " AND s.name LIKE ?";
                queryList.add("%" + queryName + "%");
            }
            String queryAge = studentQuery.getAge();
            if(queryAge != null && !"".equalsIgnoreCase(queryAge)) {
                sql += " AND s.age=?";
                queryList.add(Integer.parseInt(queryAge));
            }
            String queryGender = studentQuery.getGender();
            if(queryGender != null && !"".equalsIgnoreCase(queryGender)) {
                sql += " AND s.gender LIKE ?";
                queryList.add(queryGender);
            }
            String queryBanji = studentQuery.getBanji();
            if(queryBanji != null && !"".equalsIgnoreCase(queryBanji)) {
                sql += " AND b.name LIKE ?";
                queryList.add("%" + queryBanji + "%");
            }
            sql += " LIMIT ?,?";
            int offset = (studentQuery.getPage() - 1) * studentQuery.getLimit();
            queryList.add(offset);
            queryList.add(studentQuery.getLimit());
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                String banjiName = resultSet.getString("banjiName");
                StudentVO studentVO = new StudentVO(id, name, age, gender, banjiName);
                list.add(studentVO);
            }
            for (StudentVO studentVO : list) {
                System.out.println(studentVO);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection,preparedStatement,resultSet);
        }
        return list;
    }

    @Override
    public Integer selectTotalCount(StudentQuery studentQuery) {
        System.out.println("StudentDAOImpl.selectTotalCount");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int totalCount = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT COUNT(*) FROM student AS s LEFT JOIN banji as b ON s.banji_id=b.id WHERE 1=1";
            List<Object> queryList = new ArrayList<>();
            String queryName = studentQuery.getName();
            if(queryName != null && !"".equalsIgnoreCase(queryName)) {
                sql += " AND s.name LIKE ?";
                queryList.add("%" + queryName + "%");
            }
            String queryAge = studentQuery.getAge();
            if(queryAge != null && !"".equalsIgnoreCase(queryAge)) {
                sql += " AND s.age=?";
                queryList.add(Integer.parseInt(queryAge));
            }
            String queryGender = studentQuery.getGender();
            if(queryGender != null && !"".equalsIgnoreCase(queryGender)) {
                sql += " AND s.gender LIKE ?";
                queryList.add(queryGender);
            }
            String queryBanji = studentQuery.getBanji();
            if(queryBanji != null && !"".equalsIgnoreCase(queryBanji)) {
                sql += " AND b.name LIKE ?";
                queryList.add("%" + queryBanji + "%");
            }
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                totalCount = resultSet.getInt(1);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection,preparedStatement,resultSet);
        }
        return totalCount;
    }

    @Override
    public void deleteById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "DELETE FROM student WHERE id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            System.out.println(preparedStatement);
            int count = preparedStatement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection,preparedStatement,null);
        }
    }

    @Override
    public void add(Student student) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "INSERT INTO student(name,age,gender,banji_id) VALUES(?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,student.getName());
            preparedStatement.setInt(2,student.getAge());
            preparedStatement.setString(3,student.getGender());
            preparedStatement.setInt(4,student.getBanjiId());
            System.out.println(preparedStatement);
            int count = preparedStatement.executeUpdate();
            System.out.println("count：" + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection,preparedStatement,null);
        }
    }

    @Override
    public StudentVO selectById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        StudentVO studentVO = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT s.id,s.name,s.age,s.gender,b.name AS banjiName FROM student AS s LEFT JOIN banji AS b ON s.banji_id=b.id WHERE s.id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                String banjiName = resultSet.getString("banjiName");
                studentVO = new StudentVO(id, name, age, gender, banjiName);
            }
            System.out.println(studentVO);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection,preparedStatement,resultSet);
        }
        return studentVO;
    }

    @Override
    public void update(Student student) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "UPDATE student SET name=?,age=?,gender=?,banji_id=? WHERE id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,student.getName());
            preparedStatement.setInt(2,student.getAge());
            preparedStatement.setString(3,student.getGender());
            preparedStatement.setInt(4,student.getBanjiId());
            preparedStatement.setInt(5,student.getId());
            System.out.println(preparedStatement);
            int count = preparedStatement.executeUpdate();
            System.out.println("count：" + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection,preparedStatement,null);
        }
    }

    @Override
    public List<Student> selectAll() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Student> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,age,gender FROM student";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection,preparedStatement,resultSet);
        }
        return list;
    }
}
